package com.example.administrator.kotiln.ui

import android.app.Activity
import android.os.Bundle
import android.os.Handler
import android.os.Message

import com.example.administrator.kotiln.bean.Login_Data
import com.example.administrator.kotiln.utils.Json
import com.example.administrator.kotiln.utils.SPUtils
import com.example.administrator.kotiln.utils.Share
import com.example.administrator.kotiln.utils.ToastUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.lzy.okgo.OkGo
import com.lzy.okgo.cache.CacheMode
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response

import com.example.administrator.kotiln.R
import kotlinx.android.synthetic.main.activity_head.*

/**
 * Created by Administrator on 2017/8/23.
 */

class OkGoActivity : Activity() {

    private var login_data: Login_Data? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_okgo)
        okgo()
        okgoone()
        threads()
        initValue()
    }

    private fun initValue() {
        back.setOnClickListener {
            finish()
        }
    }


    private fun okgoone() {
        val url = "http://tangwu.com/app/login.php"
        val tel = "18737192723"
        val password = "123456"
        OkGo.patch<String>(url)
                .tag(this)
                .cacheKey("oo")
                .cacheMode(CacheMode.DEFAULT)
                .retryCount(3)
                .execute(object : StringCallback() {
                    override fun onSuccess(response: Response<String>?) {

                    }

                    override fun onError(response: Response<String>?) {
                        super.onError(response)
                    }
                })


    }


    /**
     * 登录请求网络
     *
     * @param
     * @param
     */
    private fun okgo() {
        val url = " http://tangwu.com/app/login.php"
        Share.d("网络请求" + url)
        val tel = "18737192723"
        val password = "123456"
        OkGo.post<String>(url)
                .tag(this)
                .retryCount(3)
                .cacheKey("login")
                .cacheMode(CacheMode.DEFAULT)
                .params("username", tel)
                .params("password", password)
                .execute(object : StringCallback() {
                    override fun onSuccess(response: com.lzy.okgo.model.Response<String>) {
                        Share.d("新网络请求" + response.body())
                        val result = Json.parse_message(response.body())
                        if (result.isState && result != null) {
                            //Gson解析
                            val gson = Gson()
                            login_data = gson.fromJson<Login_Data>(response.body(), object : TypeToken<Login_Data>() {

                            }.type)
                            if (login_data!!.ret == "ok") {
                                handler.sendEmptyMessage(1)
                            } else if (login_data!!.ret == "err") {
                                handler.sendEmptyMessage(2)
                            }
                        } else {
                            ToastUtils.showToast(this@OkGoActivity, result.message)
                        }
                    }

                    override fun onError(response: com.lzy.okgo.model.Response<String>) {
                        super.onError(response)
                        Share.d("错误：" + response.body())
                        handler.sendEmptyMessage(0)
                    }
                })

    }

    private fun threads() {
        Thread(Runnable {
            handler.sendEmptyMessage(3)

        }).start()
    }


    private val handler = object : Handler() {
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)

            when (msg.what) {
                0 -> ToastUtils.showToast(this@OkGoActivity, resources.getString(R.string.err_alert))
                1 -> if (login_data!!.data.toString().length > 0 && login_data!!.data != null) {
                    Share.d("token" + login_data!!.data.user_token)
                    SPUtils.put(this@OkGoActivity, "token", login_data!!.data.user_token)
                    //  String image_one = Path.NAME + login_data.getData().getPortrait();
                    //  SPUtils.put(LoginActivity.this, "image_one", image_one);
                    //  String status = String.valueOf(login_data.getData().getStatus());
                    //  String type = login_data.getData().getType();

                    //  SPUtils.put(LoginActivity.this, "status", status);
                    //  SPUtils.put(LoginActivity.this, "type", type);

                    Share.d("取token" + SPUtils.get(this@OkGoActivity, "token", "")!!)
                    //   Share.d("status" + status);
                    //ToastUtil.show(LoginActivity.this, login_data.getMsg());
                    ToastUtils.showToast(this@OkGoActivity, "登录成功")
                    // finish()
                }
                2 -> {
                }

                3 -> {
                    ToastUtils.showToast(this@OkGoActivity, "子线程")
                }
            }//   ToastUtils.showToast(LoginActivity.this, login_data.getMsg());
        }
    }
}
